/***************************************************************
 * Name:      proThread.cpp
 * Purpose:   Defines Application Frame
 * Author:    walnutcy (walnutcy@gmail.com)
 * Created:   2012-09-14
 * Copyright: walnutcy (weibo.com/walnutcy)
 * License:
 **************************************************************/
#include "proThread.h"
#include "xgsof2textMain.h"
#include "proGSOF.h"
#include "wxLogTrace.h"

#define MAIN_UI_DLG     xgsof2textDialog

extern void wxAfxLogFmt(u8 isThread, const wxChar* fmt, ...);

proThread::proThread(wxDialog* pFrm)
{
    //ctor
    p_Frame = pFrm;
}

proThread::~proThread()
{
    //dtor
}


void *proThread::Entry()
{
    MAIN_UI_DLG* pDlg = (MAIN_UI_DLG*)p_Frame;
    wxFile  file;
    u8      ch;
    u8      ret;
    proGSOF gsof;
    wxString strPath;
    wxLogTrace log;

    u8      buffer[1024]; // walter 2012.07.18 for read too slow
    ssize_t rlen;
    ssize_t curI;

    if (NULL==pDlg) return NULL;

    strPath = pDlg->m_filePath;
    if (0 == file.Open(strPath.GetData(), wxFile::read))
    {
        wxAfxLogFmt(1, _("Error Open Input File: %s\r\n"), (wxChar*)strPath.GetData());
        return NULL;
    }
    strPath += _(".log");
    log.createLogFile(strPath, 1);

    rlen = 0;
    curI = 0;
    while(1)
    {
        // check if we were asked to exit
        if ( TestDestroy() )
            break;

        if (pDlg->mFlg_reload)
        {
            if (wxFileReadBYTE(&file, &ch) < 0)// read finished
            {
                break;
            }
        }
        else
        {
            if (0==rlen)
            {
                rlen = file.Read(buffer, 1024);
                if (rlen < 0) // read finished
                    break;
                curI = 0;
            }
            ch = buffer[curI++];
            if (curI >= rlen)
            {
                rlen = 0;
            }
        }

        {
            ret = gsof.protocol(ch);
            if (ret)
            {
                gsof.decPacket(&log);
                wxThread::Sleep(5);
            }
        }

    }
    file.Close();
    log.close();

    return NULL;
}

void proThread::OnExit()
{

    return ;
}


u8 proThread::libASCII2BIN_1(char ch)
{
    u8 value;

    value = 0x00;

    if (ch >= '0' && ch <= '9')
        value = ch - '0';
    else if (ch >= 'a' && ch <= 'f')
        value = ch - 'a' + 0xA;
    else if (ch >= 'A' && ch <= 'F')
        value = ch - 'A' + 0xA;

    return value;
}

char proThread::wxFileReadBYTE(wxFile* pFile, u8* pCH)
{
    char ret = -1;

    if (NULL == pFile) return ret;
    if (NULL == pCH) return ret;


    int    len;
    char   k  = 0;
    char   ch = 0;
    u8     v  = 0;

    do
    {
        len = pFile->Read(&ch, 1);
        if (len <= 0)
            return ret;
        if ((ch==0x0A)||(ch==0x0D)||(ch==0x20))
            continue;
        k++;
        v <<= 4;
        v += libASCII2BIN_1(ch);
    }while(k<2);

    *pCH = v;
    ret  = 0;
    return ret;
}


